package com.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RequestController {


    /**
     * 请求域
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/goto")
    public String goToPage(HttpServletRequest httpServletRequest){

        httpServletRequest.setAttribute("msg","HELLO");
        return "forward:/success";
    }
    @ResponseBody
    @GetMapping("/success")
    public String success(@RequestAttribute("msg") String msg){
        return  msg;
    }

    /**
     * 矩阵变量
     * @return
     */

    // 语法：cars/sell;low=no;brand=A,B,C,D
    // Springboot默认禁用了矩阵变量
    // 手动开启 原理对于路劲的处理 UrlPathHelper 进行解析
    //       removeSemicolonContent(移除分号内容)  支持矩阵变量

    @ResponseBody
    @GetMapping("/cars/{path}")
    public Map carsSall(@MatrixVariable("low") String low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path){
        Map<String,Object> map = new HashMap<>();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }
    @ResponseBody
    @GetMapping("/cars/{path1}/over/{path2}")
    public Map carsSallto(@MatrixVariable(value = "age" , pathVar = "path1") Integer age1,
                        @MatrixVariable(value = "age", pathVar = "path2") Integer age2,
                        @PathVariable("path1") String path1,
                          @PathVariable("path2") String path2){
        Map<String,Object> map = new HashMap<>();
        map.put("path1",age1);
        map.put("path2",age2);
        return map;
    }
}
